package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.Classg;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.dao.ClassgDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;

@RestController
public class ClassgController {

    @Autowired
    ClassgDao classDao;

    @CrossOrigin
    @RequestMapping("/allClass")
    public String getClassgList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        System.out.println("nihaooooooooo");
        int numbers = classDao.getClasserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数，此处为模糊查询
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Classg> classg = classDao.getAllClassg("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        System.out.println(classg);
        res.put("numbers",numbers);
        res.put("data",classg);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println(res);
        System.out.println( users_json);
        return users_json;
    }

    @RequestMapping("/addClass")
    public String addClasser(@RequestBody Classg classg){
        System.out.println(classg);
        int i = classDao.addClasser(classg);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdateClass")
    public String getUpdateClasser(String cid){
        System.out.println("编号:"+cid);
        Classg updateClasser = classDao.getUpdateClasser(cid);
        String users_json = JSON.toJSONString(updateClasser);
        return users_json;
    }

    @RequestMapping("/editClass")
    public String editClasser(@RequestBody Classg classg){
        System.out.println(classg);
        int i = classDao.editClasser(classg);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteClass")
    public String deleteClasser(@RequestBody Classg classg){
        System.out.println(classg.getCid());
        int i = classDao.deleteClasser(classg.getCid());
        String str = i >0?"success":"error";
        return str;
    }
}
